package cn.star.jots.user.service.impl;

import cn.star.jots.base.exception.ServiceException;
import cn.star.jots.base.response.StatusCode;
import cn.star.jots.user.mapper.UserMapper;
import cn.star.jots.user.pojo.dto.UserLoginParam;
import cn.star.jots.user.pojo.dto.UserQuery;
import cn.star.jots.user.pojo.dto.UserSaveParam;
import cn.star.jots.user.pojo.entity.User;
import cn.star.jots.user.pojo.vo.UserVO;
import cn.star.jots.user.service.UserLoginService;
import cn.star.jots.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
       log.debug("用户登录:userLoginParam={}", userLoginParam);
       UserVO userVO = userMapper.selectByPhone(userLoginParam.getPhone());
       if (userVO == null) {
           throw new ServiceException(StatusCode.USER_NOT_EXIST);
       }
       if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
       log.debug("用户登录业务,结果:{}", userVO);
       return userVO;
    }

    @Override
    public UserVO backup(UserLoginParam userLoginParam) {
       log.debug("备用登录:userLoginParam={}", userLoginParam);
       UserVO userVO = userMapper.selectByEmail(userLoginParam.getEmail());
       if (userVO == null) {
           throw new ServiceException(StatusCode.USER_NOT_EXIST);
       }
       if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
       log.debug("备用登录业务,结果:{}", userVO);
       return userVO;
    }
}
